package utils

import (
	"crypto/rand"
	"math/big"
	"strconv"
	"time"
)

func RandomString(n int) string {
	var str = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
	length := len(str)
	bigInt := big.NewInt(int64(length))
	b := make([]rune, n)
	for i := range b {
		randomInt, _ := rand.Int(rand.Reader, bigInt)
		b[i] = str[randomInt.Int64()]
	}
	return string(b)
}

func RandomDigit(n int) string {
	var str = []rune("1234567890")
	length := len(str)
	bigInt := big.NewInt(int64(length))
	b := make([]rune, n)
	for i := range b {
		randomInt, _ := rand.Int(rand.Reader, bigInt)
		b[i] = str[randomInt.Int64()]
	}
	return string(b)
}

func RandInt(min, max int) int {
	if min >= max || max == 0 {
		return max
	}
	result, _ := rand.Int(rand.Reader, big.NewInt(int64(max-min)))
	return int(result.Int64()) + min
}

func WeekDay(t time.Time) string {
	weekday := t.Local().Weekday()
	var days = []string{"天", "一", "二", "三", "四", "五", "六"}
	return "星期" + days[weekday]
}

func Interface2Float64(inter interface{}) float64 {
	switch inter.(type) {
	case string:
		val, _ := strconv.ParseFloat(inter.(string), 64)
		return val
	case int:
		val := inter.(int)
		return float64(val)
	case float64:
		return inter.(float64)
	}
	return 0
}
